<template>
  <view class="login-page bg-login">
    <view class="page-content">
      <image class="logo" :src="getImageSrc('static/operation/login-logo.png')"></image>
      <button class="login-btn" open-type="getPhoneNumber" @getphonenumber="onLogin">
        <image class="wxlogo" :src="getImageSrc('static/icons/icon-weixin.png')"></image>
        <text>微信授权登录</text>
      </button>
    </view>
  </view>
</template>


<script setup>
import { getImageSrc } from '@/path/until';
import service from "@/path/service"
import { useUserInfoStore } from '@/path/store/userInfo';
import { subscribeMessage } from "@/path/until"
const userStore = useUserInfoStore()
import { router } from "@/path/router";

function onMessage() {
  return new Promise((resolve, reject) => {
    if (!userStore.userInfo.token || userStore.userInfo.is_staff != 1 || userStore.userInfo.is_notice != 1) {
      router.back({ data: userStore.userInfo })
      resolve()
    } else {
      subscribeMessage().then((res) => {
        resolve()
        if (res !== "reject") {
          return service.post("api/ycg/usercenter/setnotice")
        }
      }).catch(() => {
        reject()
      }).finally(() => {
        router.back({ data: userStore.userInfo })
      })
    }
  })
}

function onLogin({ detail }) {
  if (!detail.code) return
  uni.login().then((res) => {
    userStore.loginUserInfo({
      jscode: res.code,
      phonecode: detail.code,
      nickname: "",
      avatar: ""
    }).then(() => {
      if (!userStore.userInfo.nickname || !userStore.userInfo.avatar) {
        return router.navigate({ url: "/pages/mine/set-user/index" })
      } else {
        return Promise.resolve(userStore.userInfo)
      }
    }).then(() => {
      onMessage()
    })
  })
}
</script>

<style lang="less" scoped>
@import url(./index.less);
</style>

